package com.vf.admin.client.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.*;
import com.vf.admin.client.dto.UserAuthDto;
import com.vf.admin.client.dto.VoiceSignatureDto;
import com.vf.admin.client.service.AppAuthService;
import com.vf.admin.client.service.AppAuthUserService;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppAuth;
import com.vf.admin.miaodong.entity.AppAuthUser;
import com.vf.admin.miaodong.entity.AppMingXiZuanShi;
import com.vf.admin.miaodong.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

@Service
public class AppAuthUserServiceImpl implements AppAuthUserService {

    @Autowired
    AppAuthUserDao appAuthUserDao;
    @Autowired
    AppUserDao appUserDao;
    @Autowired
    AppMingXiZuanShiDao mingXiZuanShiDao;
    @Autowired
    AppAuthService appAuthService;
    @Autowired
    AppAuthDao appAuthDao;
    @Autowired
    AppSignatureParseLogDao appSignatureParseLogDao;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<AppAuthUser> page(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return appAuthUserDao.page(paraMap, splitPage);
    }

    @Override
    public AppAuthUser info(Integer id) {
        return appAuthUserDao.get(id, AppAuthUser.class);
    }

    @Override
    public void save(AppAuthUser appAuthUser) {
        if (appAuthUser.getId() == null || appAuthUser.getId() == 0) {
            appAuthUserDao.add(appAuthUser);
        } else {
            appAuthUserDao.update(appAuthUser);
        }
    }

    @Override
    public Integer infoByUserAndName(Integer userId, String name) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, name);
        return appAuthUser != null ? appAuthUser.getStatus() : 0;
    }

    @Override
    public AppAuthUser infoByUserAndNameB(Integer userId, String name) {
        return appAuthUserDao.infoByUserAndName(userId, name);
    }

    @Override
    public Map<String, Integer> infoMapByUser(Integer userId) {
        Map<String, Integer> data = new HashMap<>();

        Integer realAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.REAL_AUTH);
        Integer mobileAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.MOBILE_AUTH);
        Integer realNameAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.REAL_NAME_AUTH);
        Integer eduAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.EDU_AUTH);
        Integer carAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.CAR_AUTH);
        Integer houseAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.HOUSE_AUTH);
        //Integer jobAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.JOB_AUTH);
        Integer voiceAuthStatus = this.infoByUserAndName(userId, ClientConstants.UserAuth.VOICE);

        data.put(ClientConstants.UserAuth.REAL_AUTH, realAuthStatus);
        data.put(ClientConstants.UserAuth.MOBILE_AUTH, mobileAuthStatus);
        data.put(ClientConstants.UserAuth.REAL_NAME_AUTH, realNameAuthStatus);
        data.put(ClientConstants.UserAuth.EDU_AUTH, eduAuthStatus);
        data.put(ClientConstants.UserAuth.CAR_AUTH, carAuthStatus);
        data.put(ClientConstants.UserAuth.HOUSE_AUTH, houseAuthStatus);
        //data.put(ClientConstants.UserAuth.JOB_AUTH, jobAuthStatus);
        data.put(ClientConstants.UserAuth.VOICE, voiceAuthStatus);
        return data;
    }

    @Override
    public List<UserAuthDto> infoByUser(Integer userId) {
        List<UserAuthDto> list = new ArrayList<>();
        List<UserAuthDto> list1 = new ArrayList<>();
        UserAuthDto dto = null;

        try {
            Class<?> aClass = Class.forName("com.vf.admin.client.common.ClientConstants$UserAuth");
            Field[] fields = aClass.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                field.setAccessible(true);
                String fieldName = field.getName();

                dto = new UserAuthDto();
                AppAuth appAuth = appAuthDao.infoByNameEn(fieldName);
                if (ObjectUtil.isNull(appAuth)) {
                    continue;
                }
                dto.setAuthName(appAuth.getAuthName().substring(0, 2) + "未" + appAuth.getAuthName().substring(2));
                dto.setAuthNameEn(appAuth.getAuthNameEn());
                dto.setLogo(appAuth.getLogo());
                dto.setBlackLogo(appAuth.getBlackLogo());
                dto.setStatus(0);

                AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, fieldName);
                if (appAuthUser != null) {
                    if (appAuthUser.getStatus() == 2) {
                        dto.setAuthName(appAuth.getAuthName().substring(0, 2) + "已" + appAuth.getAuthName().substring(2));
                    }
                    dto.setStatus(appAuthUser.getStatus());
                }
                //控制一下排序，让已认证的排序在前
                if (dto.getStatus() == 2) {
                    list.add(dto);
                } else {
                    list1.add(dto);
                }
            }
            list.addAll(list1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public VoiceSignatureDto infoVoiceSignature(Integer fromUserId, Integer toUserId) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(toUserId, ClientConstants.UserAuth.VOICE);
        if (appAuthUser == null || appAuthUser.getStatus() != 2) {
            return new VoiceSignatureDto();
        }
        VoiceSignatureDto dto = new VoiceSignatureDto();
        if (appAuthUser != null) {
            dto.setVoiceUrl(appAuthUser.getImage());
            dto.setSeconds(Integer.valueOf(appAuthUser.getField1()));
            dto.setStatus(appAuthUser.getStatus());
        }

        Integer parseCount = appSignatureParseLogDao.signatureParseCount(toUserId);
        Integer isParse = appSignatureParseLogDao.isParse(fromUserId, toUserId);

        dto.setParseCount(parseCount);
        dto.setIsParse(isParse);
        return dto;
    }

    @Override
    public VoiceSignatureDto infoVoiceSignatureAuditStatus(Integer fromUserId, Integer toUserId) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndNameByUserIdOrTXCode(toUserId, ClientConstants.UserAuth.VOICE);
        VoiceSignatureDto dto = new VoiceSignatureDto();
        if (appAuthUser != null) {
            dto.setVoiceUrl(appAuthUser.getImage());
            dto.setSeconds(Integer.valueOf(appAuthUser.getField1()));
            dto.setStatus(appAuthUser.getStatus());
        }

        Integer parseCount = appSignatureParseLogDao.signatureParseCount(toUserId);
        Integer isParse = appSignatureParseLogDao.isParse(fromUserId, toUserId);

        dto.setParseCount(parseCount);
        dto.setIsParse(isParse);
        return dto;
    }

    @Override
    public void addUserAuthAudit(Integer userId, String name, String image, String image2, String field1, String field2) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, name);
        if (appAuthUser == null) {
            appAuthUser = new AppAuthUser();
            appAuthUser.setUserId(userId);
            appAuthUser.setAuthNameEn(name);
            appAuthUser.setImage(image);
            appAuthUser.setImage2(image2);
            appAuthUser.setField1(field1);
            appAuthUser.setField2(field2);
            appAuthUser.setStatus(1);
            appAuthUser.setCreateTime(new Date());
            appAuthUserDao.add(appAuthUser);
        } else if (appAuthUser.getStatus() == 3) {
            appAuthUser.setImage(image);
            appAuthUser.setImage2(image2);
            appAuthUser.setField1(field1);
            appAuthUser.setField2(field2);
            appAuthUser.setStatus(1);
            appAuthUserDao.update(appAuthUser);
        }
    }

    @Override
    public void addUserAuthAuditNot(Integer userId, String name, String image, String image2, String field1, String field2) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, name);
        if (appAuthUser == null) {
            appAuthUser = new AppAuthUser();
            appAuthUser.setUserId(userId);
            appAuthUser.setAuthNameEn(name);
            appAuthUser.setImage(image);
            appAuthUser.setImage2(image2);
            appAuthUser.setField1(field1);
            appAuthUser.setField2(field2);
            appAuthUser.setStatus(2);
            appAuthUser.setCreateTime(new Date());
            appAuthUserDao.add(appAuthUser);
        } else if (appAuthUser.getStatus() == 3) {
            appAuthUser.setImage(image);
            appAuthUser.setImage2(image2);
            appAuthUser.setField1(field1);
            appAuthUser.setField2(field2);
            appAuthUser.setStatus(2);
            appAuthUserDao.update(appAuthUser);
        }
    }


    /*@Override
    public void addUserAuthAuditEdu(Integer userId, String name, String image, String image2, String school, String education) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, name);
        if (appAuthUser == null) {
            appAuthUser = new AppAuthUser();
            appAuthUser.setUserId(userId);
            appAuthUser.setAuthNameEn(name);
            appAuthUser.setImage(image);
            appAuthUser.setImage2(image2);
            appAuthUser.setSchool(school);
            appAuthUser.setEducation(education);
            appAuthUser.setStatus(1);
            appAuthUser.setCreateTime(new Date());
            appAuthUserDao.add(appAuthUser);
        }
    }

    @Override
    public void addUserAuthAuditCar(Integer userId, String name, String image, String image2, String carBrand) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, name);
        if (appAuthUser == null) {
            appAuthUser = new AppAuthUser();
            appAuthUser.setUserId(userId);
            appAuthUser.setAuthNameEn(name);
            appAuthUser.setImage(image);
            appAuthUser.setImage2(image2);
            appAuthUser.setCarBrand(carBrand);
            appAuthUser.setStatus(1);
            appAuthUser.setCreateTime(new Date());
            appAuthUserDao.add(appAuthUser);
        }
    }

    @Override
    public void addUserAuthAuditVoiceSignature(Integer userId, String name, String image, Integer seconds) {
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, name);
        if (appAuthUser == null) {
            appAuthUser = new AppAuthUser();
            appAuthUser.setUserId(userId);
            appAuthUser.setAuthNameEn(name);
            appAuthUser.setImage(image);
            appAuthUser.setSeconds(seconds);
            appAuthUser.setStatus(1);
            appAuthUser.setCreateTime(new Date());
            appAuthUserDao.add(appAuthUser);
        }
    }*/

    @Override
    public void addUserAuth(Integer userId, String name, String image, String image2, String field1, String field2) {
        AppAuth appAuth = appAuthDao.infoByNameEn(name);
        AppAuthUser appAuthUser = appAuthUserDao.infoByUserAndName(userId, name);
        if (appAuthUser == null) {
            appAuthUser = new AppAuthUser();
            appAuthUser.setUserId(userId);
            appAuthUser.setAuthNameEn(name);
            appAuthUser.setImage(image);
            appAuthUser.setImage2(image2);
            appAuthUser.setField1(field1);
            appAuthUser.setField2(field2);
            appAuthUser.setStatus(2);
            appAuthUser.setCreateTime(new Date());
            appAuthUserDao.add(appAuthUser);
        }

        doGiveDiamond(userId, new BigDecimal(appAuth.getGive()));
    }

    @Override
    public void success(Integer userId, String name) {
        AppAuth appAuth = appAuthDao.infoByNameEn(name);

        appAuthUserDao.successByUserAndName(userId, name);
        if (name.equals(ClientConstants.UserAuth.VOICE)) {
            AppAuthUser appAuthUser = this.infoByUserAndNameB(userId, name);
            AppUser appUser = appUserDao.get(userId);
            appUser.setVoiceSignature(appAuthUser.getImage());
            appUserDao.update(appUser);
        }

        doGiveDiamond(userId, new BigDecimal(appAuth.getGive()));
    }

    @Override
    public void reject(Integer userId, String name, String reason) {

        appAuthUserDao.rejectByUserAndName(userId, name);

    }

    /**
     * 赠送钻石
     *
     * @param userId
     * @param num
     */
    public void doGiveDiamond(Integer userId, BigDecimal num) {
        appUserDao.updateZuanShi(userId, num);
        String key = String.format(RedisKeys.USER, userId);
        redisUtil.del(key);

        AppUser fromUser = appUserDao.get(userId);
        AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
        zuanShi.setUserid(userId);
        zuanShi.setPic(fromUser.getPic());
        zuanShi.setPhone(fromUser.getLoginname());
        zuanShi.setNick(fromUser.getNick());
        zuanShi.setLeixing(1);
        zuanShi.setNum(num);
        zuanShi.setCreatetime(new Date());
        zuanShi.setBeizhu("认证奖励");
        mingXiZuanShiDao.saveOrUpdate(zuanShi);
    }
}
